/*****************************************************************************
 * mqtt_kawaii.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "mqttclient.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_INFO
#define LOG_MODULE    "mqtt.kawaii"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

typedef struct {
    plum_mqtt_drv_write mqtt_io_wirte;
    plum_mqtt_drv_read  mqtt_io_read;
    plum_mqtt_cb        mqtt_evt_cb;
} plum_mqtt_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
PLUM_PRIVATE plum_mqtt_t mqtt_hdl;

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_void mqtt_msg_handle(plum_void *client, message_data_t *msg)
{
    PLUM_UNUSED(client);

    LOG_I("mqtt topic   :%s", msg->topic_name);
    LOG_I("mqtt payload :%s", msg->message->payload);

    plum_mqtt_msg_t message = {0};
    message.payload         = msg->message->payload;
    message.payloadLen      = msg->message->payloadlen;
    message.topic           = msg->topic_name;
    message.client          = client;
    mqtt_hdl.mqtt_evt_cb(MQTT_EVT_MSG, &message);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_mqtt_client_ref_t plum_cal_mqtt_connect(plum_s8 *client_id, plum_s8 *url,
                                             plum_u32 port, plum_s8 *usr,
                                             plum_s8 *passwd)
{
    mqtt_client_t *client = NULL;

    do {
        mqtt_log_init();

        client = mqtt_lease();
        if (client == NULL) {
            LOG_E("release mqtt err");
            break;
        }

        char port[8];
        snprintf(port, sizeof(port), "%d", port);

        mqtt_set_port(client, port);
        mqtt_set_host(client, url);
        mqtt_set_client_id(client, client_id);
        mqtt_set_user_name(client, usr);
        mqtt_set_password(client, passwd);
        mqtt_set_clean_session(client, 1);

        plum_s32 rc = mqtt_connect(client);
        if (rc) {
            LOG_E("mqtt_connect err,rc:%d", rc);
            mqtt_release(client);
            break;
        }
        PLUM_UNUSED(rc);

        LOG_I("connected mqtt broker");
    } while (0);

    return ((plum_mqtt_client_ref_t)client);
}

PLUM_PUBLIC
plum_s32 plum_cal_mqtt_disconnect(plum_mqtt_client_ref_t client)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!client) {
            rc = PLUM_ECODE_INVALID;
            LOG_E("mqtt is invalid,rc: %d", rc);
            break;
        }

        rc = mqtt_disconnect(client);
        if (rc) {
            LOG_E("mqtt_disconnect err,rc: %d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }

    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_cal_mqtt_subscribe(plum_mqtt_client_ref_t client, plum_s8 *topic)
{
    plum_s32 rc = 0;

    do {
        if (!client) {
            rc = PLUM_ECODE_INVALID;
            LOG_E("client is invalid,rc: %d", rc);
            break;
        }

        rc = mqtt_subscribe((mqtt_client_t *)client, topic, QOS0,
                            mqtt_msg_handle);
        if (rc != MQTT_SUCCESS_ERROR) {
            LOG_E("suscribe mqtt topic[%s] err,rc:%d", topic, rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_cal_mqtt_unsubscribe(plum_mqtt_client_ref_t client,
                                   plum_s8 *              topic)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!client) {
            rc = PLUM_ECODE_INVALID;
            LOG_E("client is invalid,rc: %d", rc);
            break;
        }

        if (!topic) {
            rc = PLUM_ECODE_EPARA;
            LOG_E("topic pointer is null,rc: %d", rc);
            break;
        }

        rc = mqtt_unsubscribe(client, topic);
        if (rc) {
            LOG_E("mqtt_unsubscribe err,rc: %d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_cal_mqtt_publish(plum_mqtt_client_ref_t client,
                               plum_mqtt_msg_t *      msg)
{
    plum_s32 rc = 0;

    do {
        if (!client) {
            rc = PLUM_ECODE_INVALID;
            LOG_E("client is invalid,rc: %d", rc);
            break;
        }

        if (!msg) {
            rc = PLUM_ECODE_EPARA;
            LOG_E("msg pointer is null,rc: %d", rc);
            break;
        }

        mqtt_message_t message = {0};
        message.payload        = msg;
        message.payloadlen     = sizeof(plum_mqtt_msg_t);
        message.qos            = QOS0;
        rc                     = mqtt_publish(client, msg->topic, &message);
        if (rc != MQTT_SUCCESS_ERROR) {
            LOG_E("mqtt_publish err,rc:%d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_cal_mqtt_register_callback(plum_mqtt_cb cb)
{
    plum_s32 rc = 0;

    do {
        if (!cb) {
            rc = PLUM_ECODE_EPARA;
            LOG_E("mqtt notice callback is null,rc: %d", rc);
            break;
        }

        mqtt_hdl.mqtt_evt_cb = cb;

    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_cal_mqtt_register_drv_io(plum_mqtt_drv_read  read,
                                       plum_mqtt_drv_write write)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!read || !write) {
            rc = PLUM_ECODE_EPARA;
            LOG_E("the mqtt io pointer is null,rc: %d", rc);
            break;
        }

        mqtt_hdl.mqtt_io_read  = read;
        mqtt_hdl.mqtt_io_wirte = write;

    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_void plum_cal_mqtt_yield(plum_void)
{
}
/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
